﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;

namespace SimpleAnimationBehaviors
{
    public class HOGItemFound : TriggerAction<FrameworkElement>
    {
        //Default property values
        private Duration duration = TimeSpan.FromSeconds(1);
        
        private Storyboard myStoryboard;

         public HOGItemFound()
        {
            myStoryboard = new Storyboard();
        }

        protected override void OnAttached()
        {
            base.OnAttached();
        }

        protected override void Invoke(object o)
        {
            if (myStoryboard.GetCurrentState() == ClockState.Stopped)
            {
                myStoryboard.Duration = duration;
                DoubleAnimationUsingKeyFrames OpacityAnim = new DoubleAnimationUsingKeyFrames();
                EasingDoubleKeyFrame KeyFrame01 = new EasingDoubleKeyFrame();
                KeyFrame01.KeyTime = new TimeSpan(0, 0, 0, 0, 50);
                KeyFrame01.Value = 0;
                EasingDoubleKeyFrame KeyFrame02 = new EasingDoubleKeyFrame();
                KeyFrame02.KeyTime = new TimeSpan(0, 0, 0, 0, 200);
                KeyFrame02.Value = 0;
                EasingDoubleKeyFrame KeyFrame03 = new EasingDoubleKeyFrame();
                KeyFrame03.KeyTime = new TimeSpan(0, 0, 0, 0, 250);
                KeyFrame03.Value = 1;
                EasingDoubleKeyFrame KeyFrame04 = new EasingDoubleKeyFrame();
                KeyFrame04.KeyTime = new TimeSpan(0, 0, 0, 0, 450);
                KeyFrame04.Value = 1;
                EasingDoubleKeyFrame KeyFrame05 = new EasingDoubleKeyFrame();
                KeyFrame05.KeyTime = new TimeSpan(0, 0, 0, 0, 500);
                KeyFrame05.Value = 0;
                EasingDoubleKeyFrame KeyFrame06 = new EasingDoubleKeyFrame();
                KeyFrame06.KeyTime = new TimeSpan(0, 0, 0, 0, 700);
                KeyFrame06.Value = 0;
                EasingDoubleKeyFrame KeyFrame07 = new EasingDoubleKeyFrame();
                KeyFrame07.KeyTime = new TimeSpan(0, 0, 0, 0, 750);
                KeyFrame07.Value = 1;
                EasingDoubleKeyFrame KeyFrame08 = new EasingDoubleKeyFrame();
                KeyFrame08.KeyTime = new TimeSpan(0, 0, 0, 0, 900);
                KeyFrame08.Value = 1;
                EasingDoubleKeyFrame KeyFrame09 = new EasingDoubleKeyFrame();
                KeyFrame09.KeyTime = new TimeSpan(0, 0, 0, 1, 250);
                KeyFrame09.Value = 0;
                
                OpacityAnim.KeyFrames.Add(KeyFrame01);
                OpacityAnim.KeyFrames.Add(KeyFrame02);
                OpacityAnim.KeyFrames.Add(KeyFrame03);
                OpacityAnim.KeyFrames.Add(KeyFrame04);
                OpacityAnim.KeyFrames.Add(KeyFrame05);
                OpacityAnim.KeyFrames.Add(KeyFrame06);
                OpacityAnim.KeyFrames.Add(KeyFrame07);
                OpacityAnim.KeyFrames.Add(KeyFrame08);
                OpacityAnim.KeyFrames.Add(KeyFrame09);

                myStoryboard.Duration = new TimeSpan(0, 0, 0, 1, 300);
                myStoryboard.Children.Add(OpacityAnim);
                Storyboard.SetTarget(OpacityAnim, AssociatedObject);
                Storyboard.SetTargetProperty(OpacityAnim, new PropertyPath("(Opacity)"));
                
                
                myStoryboard.Begin();
            }
        }
    }

}
